package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.LogMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpResult;
import com.itheima.pojo.OperateLog;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.LogService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.embedded.TomcatVirtualThreadsWebServerFactoryCustomizer;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class LogServiceImpl extends ServiceImpl<LogMapper, OperateLog> implements LogService {
    @Autowired
    private EmpService empService;
    @Override
    public EmpResult page(Integer page, Integer pageSize) {
        //分页查询
        Page<OperateLog> p = Page.of(page, pageSize);
        Page<OperateLog> pr = this.page(p);
        List<OperateLog> records = pr.getRecords();

        //获取日志表中所有的员工 id集合
        List<Integer> empIds = records.stream().map(OperateLog::getOperateEmpId).toList();
        if(!empIds.isEmpty()){
            //查询员工表
            List<Emp> emps = empService.listByIds(empIds);
            records.forEach(log->{
                emps.forEach(dept->{
                    if(log.getOperateEmpId()!=null && log.getOperateEmpId().equals(dept.getId()))
                        log.setOperateEmpName(dept.getName());
                });
            });
        }

        return new EmpResult(pr.getTotal(),records);
    }
}
